<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">

    <link rel="stylesheet" href="bootstrap-3.3.4/dist/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="Flat-UI-master/dist/css/flat-ui.min.css"/>
    <script src="bootstrap-3.3.4/dist/js/jquery-3.5.1.min.js"></script>
    <script src="bootstrap-3.3.4/dist/js/jquery.cookie-1.4.1.min.js"></script>
    <script src="bootstrap-3.3.4/dist/js/bootstrap.min.js"></script>
    <script src="Flat-UI-master/dist/js/flat-ui.min.js"></script>
    <script src="custom/util.js"></script>

    <title>登录</title>

    <style>
        .row {
            margin-left: 20px;
            margin-right: 20px;;
        }
    </style>

</head>


<body>


<script>

    $(function () {
        // 允许跨域传cookie
        $.ajaxSetup({crossDomain: true, xhrFields: {withCredentials: true}});

        // 登录
        $("button").click(function () {
            $.ajax({
                url: url + "/user/login",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({
                    username: $("input#username").val(),
                    password: $("input#password").val()
                }),
                // async: false,
                success: function (res) {
                    if (res.flag === 2000) {
                        // $("#common").hide();
                        location.assign("Index.html"); // 成功的话. 还要跳转到首页
                    } else {
                        $("#common").children().first().text(res.data);
                        $("#common").show();
                    }
                },
                error: function () {
                    alert("登录错误，请联系开发者解决问题")
                }
            });
        })

        // 注销
        $("#logout").click(function () {
            $("#logout").hide();
            $("#register").show();
            $("#login").show();
            $.ajax({
                url: url + "/user/logout",
                success: function (data) {
                    console.log("log out success")
                },
                error: function () {
                    console.log("error");
                }
            });
        });

        // // 查看当前登录状态
        // if ($.cookie("JSESSIONID") === null) {
        //     $("#register").show();
        //     $("#login").show();
        // } else {
        //     $("#logout").show();
        // }
        // 老的方式，再次使用
        $.ajax({
            url: url + "/user/check",
            success: function (data) {
                if (data["flag"] === 2000) {
                    // 已经登录了
                    $("#logout").show();
                } else {
                    // 还没登录
                    $("#register").show();
                    $("#login").show();
                }
            },
            error: function () {
                console.log("error");
            }
        });
    });


</script>


<!-- Static navbar -->
<div class="navbar navbar-default navbar-static-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">这什么东西</span>
            </button>
            <a class="navbar-brand" href="Index.html">快递代取系统</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="Index.html">首页</a></li>
                <li><a href="NewOrder.html">创建订单</a></li>
                <li><a href="Order.html">我的订单</a></li>
                <li><a href="PickedOrder.html">我的接单</a></li>
                <li><a href="UserInfo.html">个人中心</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right hidden-sm">
                <li><a id="login" href="Login.html" style="display: none;">登录</a></li>
                <li><a id="register" href="Register.html" style="display: none">注册</a></li>
                <li><a id="logout" href="#" style="display: none">注销</a></li>
                <li>
                    <a href="Cart.html"><span class="glyphicon glyphicon-shopping-cart">购物车</span></a>
                </li>
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</div>

<!--content-->
<div class="container">
    <div class="row thumbnail center">
        <div class="col-sm-12">
            <h1 class="text-center" style="margin-bottom: 30px">用户登录</h1>
        </div>

        <div class="col-sm-6">
            <form class="form-horizontal caption" action="http://www.baidu.com" onsubmit="return false;">
                <!--  表单 form  -->

                <!-- 用户名 -->
                <div class="form-group">
                    <label for="username" class="col-sm-3 control-label">用户名</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="username" placeholder="用户名">  <!-- username-->
                    </div>
                </div>

                <!-- 密码 -->
                <div class="form-group">
                    <label for="password" class="col-sm-3 control-label">密码</label>
                    <div class="col-sm-8">
                        <input type="password" class="form-control" id="password" placeholder="密码">  <!-- password -->
                    </div>
                </div>


                <!-- 提示信息: 用户不存在or密码错误 -->
                <div class="form-group">

                    <div class="col-sm-offset-3 col-sm-8">
                        <p id="common" style="font-size: 10px; display: none;">&nbsp;&nbsp;&nbsp;<b>msg</b></p>
                    </div>

                </div>

                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-9">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox">记住我
                            </label>
                        </div>
                    </div>
                </div>


                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-5">
                        <button type="submit" class="btn btn-success btn-block" onsubmit="return login();">登录</button>
                    </div>
                </div>
            </form>
        </div>


        <div class="col-sm-6">
            <div class="caption">
                <h3>免责声明</h3>
                <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at
                    eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
            </div>
        </div>

    </div>
</div>


<!--footer-->
<div class="navbar navbar-default navbar-static-bottom">
    <p style="text-align: center">版权声明区</p>
</div>
</body>
</html>